<?php

class UserController extends \BaseController {

	/**
	 * Controller constructor
	 */
	public function __construct() {
		$this->beforeFilter('auth', array('except' => array('getAPI', 'storeAPI', 'updateAPI', 'destroyAPI', 'getMatches', 'getGroups')));
	}
	
	/**
	 * Display a listing of users.
	 *
	 * @return Response
	 */
	public function index()
	{
		// get all the players
		$players = User::all();

		// load the view and pass the players
		return View::make('users.index')
			->with('players', $players);
	}


	/**
	 * Show the form for creating a new user.
	 *
	 * @return Response
	 */
	public function create()
	{
		//
	}
	
	/**
	 * Store a newly created user in storage from JSON posted data.
	 *
	 * @return JSON Response
	 */
	public function store()
	{
		//
	}


	/**
	 * Display the specified user.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function show($id)
	{
		//
	}


	/**
	 * Show the form for editing the specified user.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function edit($id)
	{
		//
	}


	/**
	 * Update the specified user in storage from JSON put data.
	 *
	 * @return JSON Response
	 */
	public function update()
	{
		//
	}


	/**
	 * Remove the specified user from storage by posted id.
	 *
	 * @return JSON Response
	 */
	public function destroy($id)
	{
		//
	}
	
	/*
	 * API Methods
	*/
	/**
	 * Display a listing of users in JSON format.
	 *
	 * @return JSON Response
	 */
	public function getAPI($id = null)
	{
		if (is_null($id))
			return Response::json(User::all());
		else
		{
			$user = User::find($id);
			if(is_null($user)){
				return Response::json('User not found', 404);
			} else {
				return Response::json($user);
			}
		}
	}
	
	/**
	 * Remove the specified user from storage by posted id.
	 *
	 * @return JSON Response
	 */
	public function destroyAPI($id)
	{
		$user = User::find($id);
	
		if(is_null($user))
		{
			return Response::json('User not found', 404);
		}
		$deletedUser = $user;
		$user->delete();
		return Response::json($deletedUser);
	}
	
	/**
	 * Update the specified user in storage from JSON put data.
	 *
	 * @return JSON Response
	 */
	public function updateAPI()
	{
		$updateUser = Input::json();
	
		$user = User::find($updateUser->get('id'));
		if(is_null($user)){
			return Response::json('User not found', 404);
		}
		$user->name = $updateUser->get('name');
		$user->lastname = $updateUser->get('lastname');
		$user->age = $updateUser->get('age');
		$user->sex = $updateUser->get('sex');
		$user->fanOf = $updateUser->get('fanOf');
		$user->location = $updateUser->get('location');
		$user->email = $updateUser->get('email');
		$user->FBToken = $updateUser->get('FBToken');
		$user->save();
	
		return Response::json($user);
	}
	
	/**
	 * Store a newly created user in storage from JSON posted data.
	 *
	 * @return JSON Response
	 */
	public function storeAPI()
	{
		$newUser = Input::json();
	
		$user = new User();
		$user->name = $newUser->get('name');
		$user->lastname = $newUser->get('lastname');
		$user->age = $newUser->get('age');
		$user->sex = $newUser->get('sex');
		$user->fanOf = $newUser->get('fanOf');
		$user->location = $newUser->get('location');
		$user->email = $newUser->get('email');
		$user->FBToken = $newUser->get('FBToken');
		$user->save();
	
		return Response::json($user);
	}
	
	/**
	 * Display a listing of matches by user id in JSON format.
	 *
	 * @param $userId
	 * @return JSON Response
	 */
	public function getMatches($userId)
	{
		$user = User::find($userId);
		if(is_null($user)){
			return Response::json('User not found', 404);
		} else {
			foreach($user->teams as $key => $value){
				$matches = $value->matches;
				$value['matches'] = $matches;
			}
			
			return Response::json(array(
					"Sub" => $user->matchesSub,
					"Guest" => $user->matchesGuest,
					"Admin" => $user->matchesAdmin,
					"Comfirmed" => $user->teams
				));
		}
	}
	
	/**
	 * Display a listing of groups by user id in JSON format.
	 *
	 * @param $userId
	 * @return JSON Response
	 */
	public function getGroups($userId)
	{
		$user = User::find($userId);
		if(is_null($user)){
			return Response::json('User not found', 404);
		} else {
			return Response::json(array(
					"Member" => $user->groups,
					"Admin" => $user->groupsAdmin
				));
		}
	}

}
